﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class BgmProjectDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public BgmProjectDAL()
        {

        }
        public DataSet LoadBgmProjects(Nullable<int> Sequence, Nullable<int> JobSiteSeq, Nullable<int> ScopeSeq, Nullable<int> BgmYear, string SearchText)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@JobSiteSeq", JobSiteSeq);
                dbManager.AddParameters(2, "@ScopeSeq", ScopeSeq);
                dbManager.AddParameters(3, "@BgmYear", BgmYear);
                dbManager.AddParameters(4, "@SearchText", SearchText);
                dbManager.AddParameters(5, "@ApproveRecord", SystemInfo._ApproveRecordStatus);
                dbManager.AddParameters(6, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT op.BGM_PROJ_OPER_SEQ, op.BGM_PROJ_OPER_CODE, op.BGM_PROJ_OPER_NAME, op.BGM_PROJ_OPER_DESCR"
                      + ", op.BGM_PROJ_OPER_DATE, op.START_DATE, op.END_DATE, op.ATTEND_COUNT, op.BGM_PROJ_SEQ, op.EMP_SEQ"
                      + ", op.JOB_SITE_SEQ, op.IS_USE, op.WORK_PLACE, op.BGM_YEAR, js.JOB_SITE_NAME"
                    + " FROM BGM_PROJECT_OPERATION op"
                    + " LEFT JOIN BGM_PROJECT pj ON op.BGM_PROJ_SEQ = pj.BGM_PROJ_SEQ"
                    + " LEFT JOIN HRM_MP_JOB_SITES js ON op.JOB_SITE_SEQ = js.JOB_SITE_SEQ"
                    + " WHERE op.RECORD_STATUS = @ApproveRecord"
                    + (Sequence.HasValue ? " AND op.BGM_PROJ_OPER_SEQ = @Sequence" : "")
                    + (JobSiteSeq.HasValue ? " AND op.JOB_SITE_SEQ = @JobSiteSeq" : "")
                    + (ScopeSeq.HasValue ? " AND pj.BGM_PROJ_SCOPE = @ScopeSeq" : "")
                    + (BgmYear.HasValue ? " AND op.BGM_YEAR = @BgmYear" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);                

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
    }
}
